package com.jt;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;



import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.time.Duration;

@Configuration
public class Redisconfig extends CachingConfigurerSupport{

    /**
     * 定义缓存key生成器，不定义也可以使用默认的
     * @return
     */
    @Bean
    @Override
    public KeyGenerator keyGenerator(){

        return new KeyGenerator(){

            @Override
            public Object generate(Object targer, Method method,Object... params){

                StringBuilder sb = new StringBuilder();
                sb.append(targer.getClass().getName());
                sb.append("::");
                sb.append(method.getName());
                for (Object param : params){

                    sb.append(param);

                }

                return  sb.toString();

            }

        };

    }
    /**
     *初始化一个Cache管理器对象,不定义也可以，有默认的，但假如 希望在基于AOP
     * 反方式实现Redis
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisCacheManager cacheManager(
        RedisConnectionFactory connectionFactory){

        RedisCacheConfiguration config =
                RedisCacheConfiguration.defaultCacheConfig()

        .entryTtl(Duration.ofSeconds(60*60))

        //配置key的序列化
        .serializeKeysWith(
                RedisSerializationContext.SerializationPair.fromSerializer(
                        new StringRedisSerializer()))

        //配置值的序列化
        .serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(
                        new Jackson2JsonRedisSerializer<Object>(Object.class)))

        .disableCachingNullValues();

        return RedisCacheManager
                .builder(connectionFactory)
                .cacheDefaults(config)//修改默认配置
                .transactionAware()//启动默认事务
                .build();

    }

    //代码定制参考RedisAutoConfiguration类
    @Bean
    @ConditionalOnMissingBean(
            name = {"redisTemplate"}
    )
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

        System.out.println("===redisTemplate===");
        RedisTemplate<Object,Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        //定义redisTemplate对象的序列化方式
        //1.定义key的序列化方式
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        //2.定义Value的序列化方式
        Jackson2JsonRedisSerializer jsonRedisSerializer=
                new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL,
                JsonAutoDetect.Visibility.ANY);

        //激活默认类型（存储json时会添加类型信息到json串中
//       objectMapper.activateDefaultTyping(
//                objectMapper.getPolymorphicTypeValidator(),
//                ObjectMapper.DefaultTyping.NON_FINAL,
//                JsonTypeInfo.As.PROPERTY);
        jsonRedisSerializer.setObjectMapper(objectMapper);
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);

        //3.修改redisTemplate默认特性设置（除了序列化，其他原有特性不丢）
        template.afterPropertiesSet();
        return template;

    }

}
